<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>主页编辑器</title>
    <#assign base=request.contextPath  />
    <link rel="stylesheet" href="${base}/css/index.css">
    <script type="text/javascript" charset="utf-8" src="${base}/ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="${base}/ueditor/ueditor.all.min.js"> </script>
    <script type="text/javascript" charset="utf-8" src="${base}/ueditor/lang/zh-cn/zh-cn.js"></script>
    <script type="text/javascript" charset="utf-8" src="${base}/js/index.js"></script>

    <style type="text/css">
        div{
            width:100%;
        }
    </style>
</head>
<body>
    <div>
        ${Session["user"].userName} 欢迎登录。  <a href="/logout">退出</a><br>
        <hr>
        <strong>标题:</strong><input type="text" name="title" id="title">
        <script id="editor" type="text/plain" style="width:1024px;height:500px;"></script>
    </div>
    <div>
        <button onclick="submitText()">提交</button>
    </div>
    <script src="${base}/js/jquery.min.js"></script>
    <script src="${base}/js/jquery.base64.js"></script>
    <script type="text/javascript">
        $.base64.utf8encode = true;
        $(function () {
            var ue = UE.getEditor('editor');
            function isFocus(e){
                alert(UE.getEditor('editor').isFocus());
                UE.dom.domUtils.preventDefault(e)
            }
            function setblur(e){
                UE.getEditor('editor').blur();
                UE.dom.domUtils.preventDefault(e)
            }
        });

        function submitText() {
            if (!UE.getEditor('editor').hasContents()) {
                alert("内容为空");
                return;
            }
            var title = $("#title").val();
            // var b = new Base64();
            var str = $.base64.btoa(UE.getEditor('editor').getContent());
            $.ajax({
                url: "/texts/submit",
                type: "POST",
                async: false,
                data: {
                    title: title,
                    body: str
                },
                success: function (data) {
                    if (data == "SUCCESS") {
                        alert("提交成功！")
                    }else{
                        alert("提交失败："+data);
                    }
                },
                error: function (data) {
                    alert("服务请求异常")
                }
            });
        }
    </script>
</body>
</html>